package com.yc.cloud.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.cloud.business.SysMenuBusiness;
import com.yc.cloud.common.model.response.Outcome;
import com.yc.cloud.model.find.FindMenuPage;
import com.yc.cloud.model.find.FindMenuTree;
import com.yc.cloud.model.save.SaveMenu;
import com.yc.cloud.model.update.ResetRoleMenu;
import com.yc.cloud.model.update.UpdateMenu;
import com.yc.cloud.sys.entity.SysMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 杨智杰
 * @since 2021/8/18 13:06
 */
@RestController
@Api(tags = "菜单相关接口")
@RequestMapping("/menu")
public class SysMenuApi {

    @Resource
    private SysMenuBusiness sysMenuBusiness;

    @ApiOperation("查询菜单树")
    @GetMapping("/tree")
    public Outcome tree(FindMenuTree param) {
        List<Tree<String>> menuTree = sysMenuBusiness.getMenuTree(param);
        return CollUtil.isNotEmpty(menuTree)
                ? Outcome.success(menuTree)
                : Outcome.failure("未查询到相关数据!");
    }


    @GetMapping
    @ApiOperation("查询")
    public Outcome<IPage<SysMenu>> page(FindMenuPage param) {
        return Outcome.success(sysMenuBusiness.page(param));
    }

    @PostMapping
    @ApiOperation("新增")
    public Outcome save(@RequestBody @Valid SaveMenu param) {
        return sysMenuBusiness.save(param);
    }

    @PutMapping
    @ApiOperation("修改")
    public Outcome update(@RequestBody @Valid UpdateMenu param) {
        return sysMenuBusiness.update(param);
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Outcome delete(@RequestBody @Valid @Size(min = 1) List<Long> id) {
        return Outcome.status(sysMenuBusiness.delete(id));
    }

    @PostMapping("/reset")
    @ApiOperation("重置角色菜单关联")
    public Outcome reset(@RequestBody @Valid ResetRoleMenu param){
        return sysMenuBusiness.reset(param);
    }

}
